package fr.castorhoz.isi_connectivity_widget;

import java.util.ArrayList;
import java.util.List;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.util.Log;
import android.widget.RemoteViews;
import fr.castorhoz.isi_connectivity_widget.BluetoothWidget.BluetoothButton;
import fr.castorhoz.isi_connectivity_widget.GPSWidget.GPSButton;
import fr.castorhoz.isi_connectivity_widget.WifiWidget.WifiButton;
import fr.castorhoz.isi_connectivity_widget.database.DBAdapter;
import fr.castorhoz.isi_connectivity_widget.widget_configuration.WidgetConfiguration;

public class ConnWidget extends AppWidgetProvider {
	private static List<ConnButton> listButtons = new ArrayList<ConnButton>();
	private static boolean isFirst = true;
	
	private DBAdapter db;

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {

		Log.i(this.getClass().toString(), "onUpdate ConnWidget");

		if (isFirst) {
			/*listButtons = new ArrayList<ConnButton>();
			listButtons.add(new WifiButton(context));
			listButtons.add(new GPSButton(context));
			listButtons.add(new BluetoothButton(context));*/
			isFirst = false;
	        db = new DBAdapter(context);
	        listButtons = getWidgetsFromPreferences(context);
			//listButtons = getWidgetsFromDatabse(context);
	        //Database configuration
		}

		for (int i = 0; i < appWidgetIds.length; i++) {
			int appWidgetId = appWidgetIds[i];
			updateAppWidget(context, appWidgetManager, appWidgetId);
		}

		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}

	private List<ConnButton> getWidgetsFromPreferences(Context context) {
		List<ConnButton> widgets = new ArrayList<ConnButton>();
		SharedPreferences sp = context.getSharedPreferences(WidgetConfiguration.ISI_CONNECTIVITY_WIDGET_PREFERENCES, Context.MODE_PRIVATE);
		for (String widgetName : sp.getAll().keySet()) {
			widgets.add(createWidgetButton(widgetName, context));
		}
 		return widgets;
	}

	private List<ConnButton> getWidgetsFromDatabse(Context context) {
		List<ConnButton> widgets = new ArrayList<ConnButton>();
		db.open();
		Cursor c = db.getWidgets();
		while (c.moveToNext()) {
			String name = c.getString(c.getColumnIndexOrThrow("widgetName"));
			widgets.add(createWidgetButton(name, context));
		}
		db.close();
		return widgets;
	}

	private ConnButton createWidgetButton(String name, Context context) {
		ConnButton button = null;
		switch (ConnButton.widgets.valueOf(name)) {
		case bluetoohWidget:
			button = new BluetoothButton(context);
			break;
		case gpsWidget:
			button = new GPSButton(context);
			break;
		case mobileDataWidget:
			//button = new MobileDataButton(context);
			break;
		case roamingWidget:
			//button = new RoamingButton(context);
			break;
		case wifiWidget:
			button = new WifiButton(context);
			break;
		default:
			break;
		}
		return button;
	}

	private void updateAppWidget(Context context,
			AppWidgetManager appWidgetManager, int appWidgetId) {
		Log.i(this.getClass().toString(), "Update Appwidget");

		// remote views
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.widget_layout);
		// update image of wifi
		for (ConnButton but : listButtons) {
			/*ImageButton ib = new ImageButton(context);
			ib.setImageResource(but.getSelectorOn());*/
			RemoteViews nestedView = new RemoteViews(context.getPackageName(), R.layout.widget_row);
			nestedView.setImageViewResource(R.id.imageButtonWidgetRow, but.getSelectorOn());
			remoteViews.addView(remoteViews.getLayoutId(), nestedView);
			Intent intent = new Intent(context, ConnWidget.class);
			intent.setAction(but.getAction());
			PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
					0, intent, 0);
			remoteViews
					.setOnClickPendingIntent(R.id.imageButtonWidgetRow, pendingIntent);
		}

		appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		System.gc();
		Log.i(this.getClass().toString(), "on receive connWidget action = " + intent.getAction());
		
		for(ConnButton but : listButtons)
		{
			if (intent.getAction().equals(but.getAction())){
				AndroidTools.afficherMessage(context, "clicked : "+but.getAction());
				Log.i(this.getClass().toString(), "switch network!");
				but.switchNetwork(context);
			}
			
		}
		
		super.onReceive(context, intent);
	}

}
